package com.springcloud.demo.serverb.controller;

import com.springcloud.demo.base.vo.ResultVO;
import com.springcloud.demo.serverb.service.ServerBServiceImpl;
import io.seata.core.context.RootContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
@RequestMapping("/server/b")
public class ServerBController {

    @Autowired
    private Environment env;

    @Autowired
    private ServerBServiceImpl serverBService;

    @RequestMapping("/get")
    public ResultVO<String> get() throws Exception {
        Random random = new Random();
        if (random.nextInt(10) % 2 == 0) {
            throw new Exception("测试异常");
        }
        return ResultVO.ok("这是服务B，端口：" + env.getProperty("server.port"));
    }

    @RequestMapping("/insert")
    public ResultVO<String> insert() throws Exception {
        System.out.println(RootContext.getXID());
        serverBService.insert();
        return ResultVO.ok("这是服务B插入");
    }
}
